//
//  BaseAnimViewController.m
//  OCAnimation
//
//  Created by zhouLiHuan on 2017/2/17.
//  Copyright © 2017年 zhouLiHuan. All rights reserved.
//

#import "BaseAnimViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface BaseAnimViewController ()

@end

@implementation BaseAnimViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor whiteColor];
    [self callBaseAnimation];
}

//调用基本动画方法
- (void)callBaseAnimation{
  
    [self transitionX]; //沿X轴旋转
    [self transitionY]; //沿Y轴旋转
    [self transitionZ]; //沿Z轴旋转
    [self moveAnim];   //移动动画
    [self backColorChange]; //背景颜色变化
    [self contentImgChange]; //内容图片变化
    [self cornerRadiusChange];//圆角变换动画
    [self imgViewScaleChange]; //比例缩放动画
    [self imgViewScaleXChange]; //比例缩放X
    [self imgViewScaleYChange]; //比例缩放Y
    [self appointBoundsScale]; //指定大小缩放
    [self alphaViewChange];  //透明视图
    
  
}
/**
 沿着x轴旋转的动画
 */
- (void)transitionX{
    
    //view
    UIView *rorationViewX = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 70, 70)];
    rorationViewX.backgroundColor = [UIColor redColor];
    [self.view addSubview:rorationViewX];
    
    //动画
    CABasicAnimation *rorationXAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    rorationXAnim.beginTime = 0.0;
    rorationXAnim.toValue = @(2*M_PI);
    rorationXAnim.duration = 1.5;
    rorationXAnim.repeatCount = HUGE_VALF;
    [rorationViewX.layer addAnimation:rorationXAnim forKey:@"rorationXAnim"];
    
    
}

/**
 沿着Y轴旋转动画
 */
- (void)transitionY{
    UIView *rorationViewY = [[UIView alloc]initWithFrame:CGRectMake(150, 100, 70, 70)];
    rorationViewY.backgroundColor = [UIColor redColor];
    [self.view addSubview:rorationViewY];
    
    //创建动画
    CABasicAnimation *rorationAnimY = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    rorationAnimY.beginTime = 0.0;
    rorationAnimY.toValue = @(2*M_PI);
    rorationAnimY.duration = 1.5;
    rorationAnimY.repeatCount = HUGE_VALF;
    [rorationViewY.layer addAnimation:rorationAnimY forKey:@"rorationAnimY"];
    
}

/**
 沿着Z轴旋转
 */
- (void)transitionZ{
    UIView *rorationViewZ = [[UIView alloc] initWithFrame:CGRectMake(280, 100, 70, 70)];
    rorationViewZ.backgroundColor = [UIColor redColor];
    [self.view addSubview:rorationViewZ];
    
    //创建动画
    CABasicAnimation *rorationAnimZ = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rorationAnimZ.beginTime = 0.0;
    rorationAnimZ.toValue = @(2*M_PI);
    rorationAnimZ.duration = 1.5;
    rorationAnimZ.repeatCount = HUGE_VALF;
    [rorationViewZ.layer addAnimation:rorationAnimZ forKey:@"rorationAnimZ"];
    
    
    
}
/**
 移动动画
 */
- (void)moveAnim{
    UIView *moveView = [[UIView alloc] initWithFrame:CGRectMake(20, 240, 70, 70)];
    moveView.center = CGPointMake(40, 200);
    moveView.backgroundColor = [UIColor redColor];
    [self.view addSubview:moveView];
    
    //动画
    CABasicAnimation *moveAnim = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnim.fromValue = [NSValue valueWithCGPoint:CGPointMake(40, 240)];
    moveAnim.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 240)];
    moveAnim.autoreverses = YES; //自动翻转
    moveAnim.duration = 2;
    moveAnim.repeatCount = HUGE_VALF;
   
    //控制view动画结束后 停留在动画结束为止
    [moveAnim setRemovedOnCompletion:YES];
    moveAnim.fillMode = kCAFillModeForwards;
    [moveView.layer addAnimation:moveAnim forKey:@"moveAnim"];
    
}

/**
 背景颜色渐变
 */
- (void)backColorChange{
    
    UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(20, 310, 70, 70)];
    colorView.backgroundColor = [UIColor redColor];
    [self.view addSubview:colorView];
    
    //动画
    CABasicAnimation *colorAnim = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    colorAnim.fromValue = CFBridgingRelease([UIColor redColor].CGColor);
    colorAnim.toValue = CFBridgingRelease([UIColor greenColor].CGColor);
    colorAnim.autoreverses = YES;
    colorAnim.duration = 2;
    colorAnim.repeatCount = HUGE_VALF;
    
    [colorView.layer addAnimation:colorAnim forKey:@"colorAnim"];
    
    
}

/**
 内容变化图画
 */
- (void)contentImgChange{
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(150, 310, 70, 70)];
    imgView.image = [UIImage imageNamed:@"from"];
    [self.view addSubview:imgView];
    
    CABasicAnimation *contentImg = [CABasicAnimation animationWithKeyPath:@"contents"];
    contentImg.toValue = (__bridge id _Nullable)([UIImage imageNamed:@"to"].CGImage);
    contentImg.duration = 2;
    contentImg.autoreverses = YES;
    contentImg.repeatCount = HUGE_VALF;
    [imgView.layer addAnimation:contentImg forKey:@"contentImg"];
                     
    
}

/**
 圆角变换动画
 */
- (void)cornerRadiusChange{
    UIView *cornerRadiusView = [[UIView alloc] initWithFrame:CGRectMake(280, 310, 70, 70)];
    cornerRadiusView.backgroundColor = [UIColor redColor];
    [self.view addSubview:cornerRadiusView];
    
    CABasicAnimation *cornerRadiusAnim = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
    cornerRadiusAnim.toValue = @(35);
    cornerRadiusAnim.duration = 2;
    cornerRadiusAnim.autoreverses = YES;
    cornerRadiusAnim.repeatCount = HUGE_VALF;
    [cornerRadiusView.layer addAnimation:cornerRadiusAnim forKey:@"cornerRadiusAnim"];
    
    
}

/**
 比例缩放动画
 */

- (void)imgViewScaleChange{
    UIView *scaleView = [[UIView alloc] initWithFrame:CGRectMake(20, 410, 70, 70)];
    scaleView.backgroundColor = [UIColor redColor];
    [self.view addSubview:scaleView];
    
    CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnim.fromValue = @(0.3);
    scaleAnim.toValue = @(1.3);
    scaleAnim.duration = 2;
    scaleAnim.autoreverses = YES;
    scaleAnim.repeatCount = HUGE_VALF;
    [scaleView.layer addAnimation:scaleAnim forKey:@"scaleAnim"];
}

/**
 比例缩放动画X
 */
- (void)imgViewScaleXChange{
    
    UIView *scaleViewX = [[UIView alloc] initWithFrame:CGRectMake(150, 410, 70, 70)];
    scaleViewX.backgroundColor = [UIColor redColor];
    [self.view addSubview:scaleViewX];
    
    CABasicAnimation *scaleXAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
    scaleXAnim.fromValue = @(0.3);
    scaleXAnim.toValue = @(1.3);
    scaleXAnim.duration = 2;
    scaleXAnim.autoreverses = YES;
    scaleXAnim.repeatCount = HUGE_VALF;
    [scaleViewX.layer addAnimation:scaleXAnim forKey:@"scaleXAnim"];
}

/**
 比例缩放动画Y
 */
- (void)imgViewScaleYChange{
    UIView *scaleViewY = [[UIView alloc] initWithFrame:CGRectMake(280, 410, 70, 70)];
    scaleViewY.backgroundColor = [UIColor redColor];
    [self.view addSubview:scaleViewY];
    
    CABasicAnimation *scaleYAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
    scaleYAnim.fromValue = @(0.3);
    scaleYAnim.toValue = @(1.3);
    scaleYAnim.duration = 2;
    scaleYAnim.autoreverses = YES;
    scaleYAnim.repeatCount = HUGE_VALF;
    [scaleViewY.layer addAnimation:scaleYAnim forKey:@"scaleYAnim"];
    
}

/**
 指定大小缩放
 */
- (void)appointBoundsScale{
    UIView *boundsView = [[UIView alloc] initWithFrame:CGRectMake(40, 520, 20, 80)];
    boundsView.backgroundColor = [UIColor redColor];
    [self.view addSubview:boundsView];
    
    CABasicAnimation *boundsAnim = [CABasicAnimation animationWithKeyPath:@"bounds"];
    boundsAnim.toValue = [NSValue valueWithCGRect:CGRectMake(800, 500, 90, 30)];
    boundsAnim.duration = 2;
    boundsAnim.autoreverses = YES;
    boundsAnim.repeatCount = HUGE_VALF;
    [boundsView.layer addAnimation:boundsAnim forKey:@"boundsAnim"];
    
}

/**
 透明视图
 */
- (void)alphaViewChange{
    
    UIView *alphaView = [[UIView alloc] initWithFrame:CGRectMake(150, 520, 70, 70)];
    alphaView.backgroundColor = [UIColor redColor];
    [self.view addSubview:alphaView];
    
    CABasicAnimation *alphaAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    alphaAnim.fromValue = @(0.3);
    alphaAnim.toValue = @(1);
    alphaAnim.duration = 0.6;
    alphaAnim.autoreverses = YES;
    alphaAnim.repeatCount = HUGE_VALF;
    [alphaView.layer addAnimation:alphaAnim forKey:@"alphaAnim"];
}



@end
